﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace kiosk
{
    public partial class LoaderForm : Form
    {
        private string msg;
        public LoaderForm()
        {
            InitializeComponent();
        }

        
        
        // the function that loads the application forms
        private void loadForm()
        {

            
            ApplicationCache.AppPdfViewer = new PdfViewer(ApplicationCache.AppDataHandler);
           
            ApplicationCache.AppFormDisplayButton = new DisplayButtons(ApplicationCache.AppDataHandler);
           
            ApplicationCache.AppMainFormWindow = new MainForm();
        
        
        
        
        
        }
        
        private void LoaderForm_Shown(object sender, EventArgs e)
        {
            
             
             bool imageFound = false;
              List<String> images;
            

            // Do the Loading jobs of the project ...........
            // First job to check the server settings in the registry if there is no settings available then ask the user to add new settings 
             msg = "Checking Application settings ......";
             this.pictLabelBox.Invalidate(this.pictLabelBox.ClientRectangle);
             this.Refresh();

            string appsettingpath = System.IO.Path.Combine(Application.StartupPath,GeneralOpt.AppSettingsFileName);

             if (!File.Exists(appsettingpath))
             { 
               // show the setting window 
                 this.TopMost = false;
                 SettingsForm sf = new SettingsForm(true);
                 sf.StartPosition = FormStartPosition.CenterScreen;
                 sf.ShowDialog();
                 this.TopMost = true;
                 this.Refresh();
             
             }


             this.loadProgress.Value = 10;

            // now loading the settings

             msg = " Loading Application Settings .....";
             this.pictLabelBox.Invalidate();
             this.pictLabelBox.Refresh();

             if (GeneralOpt.loadAppSetting() == -1)
             {
                 this.TopMost = false;
                 MessageBox.Show("Incorrect Settings file !!! \r\n Error Description : Setting Xml file Loading error");
                 this.TopMost = false;
                 SettingsForm sf = new SettingsForm(true);
                 sf.StartPosition = FormStartPosition.CenterScreen;
                 sf.ShowDialog();
                 this.TopMost = true;
                 this.Refresh();
                 //this.Close();    
                 if(GeneralOpt.loadAppSetting() == -1)
                 {
                     this.Close();
                 
                 }

             
             }

             try
             {
                 ApplicationCache.AppDataHandler = new DataBaseHandler();
                 ApplicationCache.AppDataHandler.parpreConn();
             }
             catch(Exception ex)
             {
                 MessageBox.Show("Crital error : Error msg "+ex.Message);
                 this.Close();
             
             
             }

             ApplicationCache.loadTickerMessages();
             this.loadProgress.Value += 40;
            // Checking the database for available data
            /// 
            msg = " Loading Margins ...." ;
            this.pictLabelBox.Invalidate(); 
            /// Incomplete 
            /// 
            ApplicationCache.loadMarginsSettings();

           
            //MessageBox.Show(ApplicationCache.mLeftMargin.ToString() + ApplicationCache.mTopMargin.ToString()+ApplicationCache.mRightMargin.ToString()+ApplicationCache.mButtomMargin.ToString());

            /// the logic for the form loading 
            /// 
            
            msg = " caching the labels ........";
            this.pictLabelBox.Invalidate();
            /// load all the labels in the application
            /// 
            // Incomplete

            /// The main job of the day
            /// Load the form and preprocess the images folder to create cache of images 
            /// 
            
            msg = "Checking for the image folder ......";
            this.pictLabelBox.Invalidate();

            if (!Directory.Exists(Path.Combine(Application.StartupPath, GeneralOpt.AppPictFolder)))
            {
                // if the dict does not exists so we have to
                // create it
                Directory.CreateDirectory(Path.Combine(Application.StartupPath, GeneralOpt.AppPictFolder));
                // now create a Cache Picture Diectory
                if (Directory.Exists(Path.Combine(Application.StartupPath, GeneralOpt.AppPictCacheFolder)))
                    Directory.Delete(Path.Combine(Application.StartupPath, GeneralOpt.AppPictCacheFolder));

                Directory.CreateDirectory(Path.Combine(Application.StartupPath, GeneralOpt.AppPictCacheFolder));


            }
            else
            { 
              // The picture preprocessing job
              /// The Picture Preprocessing 
              /// 
                msg = " looking for the images .....";
                this.pictLabelBox.Invalidate();
                images = new List<string>();
                #region folderSearch

                string[] path1 = Directory.GetFiles(Path.Combine(Application.StartupPath, GeneralOpt.AppPictFolder),"*.jpg");
                string[] path2 = Directory.GetFiles(Path.Combine(Application.StartupPath, GeneralOpt.AppPictFolder), "*.jpeg");
                string[] path3 = Directory.GetFiles(Path.Combine(Application.StartupPath, GeneralOpt.AppPictFolder), "*.bmp");

                foreach (string str in path1)
                {
                    images.Add(str);

                }
                foreach (string str in path2)
                {
                    images.Add(str);

                }
                foreach (string str in path3)
                {
                    images.Add(str);

                }

                #endregion 

                this.pictLabelBox.Text = " Found "+ images.Count.ToString() +" images ";
                if (images.Count > 1)
                imageFound = true;
                

            }


            // Loading the Application
            /// Application laoding]
            /// 
            this.loadProgress.Value += 10;
            msg = "Loading the Application forms ..... plz wait";
            this.pictLabelBox.Invalidate();
            this.loadForm();
            
            // Application Load and image processing
            /// Image Processing
            /// 
            if (imageFound)
            {
                // Resizing the I,ages
                /// Resizing the images
                /// 
                msg = " Rezing the images please wait .....";
                this.pictLabelBox.Invalidate();

            }

            this.loadProgress.Value += 10; 
            // Checking bgImages settings
            if (GeneralOpt.checkBgSettings() == -1)
              {
                  this.TopMost = false;
                  BackGroundImages imgset = new BackGroundImages();
                  imgset.ShowDialog();
                  this.TopMost = true;
                  this.Refresh();
            
              }
            
            
            
            
            
            // Now load the backgroung images 
            /// background images loading job

            this.loadProgress.Value += 20;
            msg = "Loading background images ......";

            this.pictLabelBox.Invalidate(this.pictLabelBox.ClientRectangle);
            this.pictLabelBox.Refresh();
            
            ApplicationCache.LoadBgImages();
           
            //
          
            this.TopMost = false;
            this.Hide();
            if (ApplicationCache.AppMainFormWindow != null)
            {
                
                ApplicationCache.AppMainFormWindow.ShowDialog();
            }
            else
            {
                MessageBox.Show("Application loading failure !!");
             
            }
            
            //


            
              

            // 1St folder is Picture folder


           this.Close();


        }

        private void LoaderForm_Load(object sender, EventArgs e)
        {



        }

        
        private void pictLabelBox_Paint(object sender, PaintEventArgs e)
        {

            if (this.msg != null)
            {
                TextRenderer.DrawText(e.Graphics, msg, new Font("Arial",8, FontStyle.Regular), new Point(0, 0), Color.Black);
            
            }



        }

       

      
       
    }
}
